package com.lanhao.aot.carrierintegration.espace;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.DigestUtils;

import com.aliyun.openservices.shade.com.alibaba.fastjson.JSON;
import com.lanhao.aot.carrierintegration.commondto.ApiRequestDTO;
import com.lanhao.aot.carrierintegration.commondto.LabelApplyRequestDTO;
import com.lanhao.aot.carrierintegration.commondto.LabelCancelRequestDTO;
import com.lanhao.aot.carrierintegration.commondto.LabelQryRequestDTO;
import com.lanhao.aot.carrierintegration.commondto.LabelTrackRequestDTO;

import cn.hutool.core.lang.UUID;
import cn.hutool.http.HttpUtil;

public class ESpaceApiTest {
	private static final Logger LOG = LoggerFactory.getLogger(ESpaceApiTest.class);
	public static final String partnerId = "e7b1e52699";
	public static final String partnerKey = "cacc3bbbc143478f8ef9b2e49fdf78c6";
	public static final String labelUrl = "http://60.205.176.122/shipments/api/v1/realshipments";
	public static final String cancelUrl = "http://127.0.0.1:8282/shipments/api/v1/labelCancel";
	public static final String trackUrl = "http://127.0.0.1:8282/shipments/api/v1/labelTrack";
	public static final String queryUrl = "http://127.0.0.1:8282/shipments/api/v1/getLabelResult";

	public static void main(String[] args) {
		doApply("NL");
//		doCancel();
//		doTrack();
//		doLabelResult();
	}

	private static void doApply(String countryCode) {
		LabelApplyRequestDTO shipmentsRequestDTO = new LabelApplyRequestDTO();
		shipmentsRequestDTO.setFromCountryCode("UK");
		shipmentsRequestDTO.setFromName("KunKun");
		shipmentsRequestDTO.setFromFirm("");
		shipmentsRequestDTO.setFromAddress1("32 minstead road");
		shipmentsRequestDTO.setFromZip5("b248pt");
		shipmentsRequestDTO.setFromCity("birmingham");
		shipmentsRequestDTO.setFromState("west midlands");
		shipmentsRequestDTO.setFromPhone("07557441041");

		if("RU".equals(countryCode)) {
			// 俄罗斯参数
			shipmentsRequestDTO.setToName("Евгений Ринатович");
			shipmentsRequestDTO.setToCity("Подольск");
			shipmentsRequestDTO.setToState("Московская область");
			shipmentsRequestDTO.setToAddress1("Московская область, город Подольск, ул.Станционная 22.");
			shipmentsRequestDTO.setToZip5("385011");
			shipmentsRequestDTO.setToPhone("+7(999)861-49-23");
		} else if("PL".equals(countryCode)) {
			// 波兰参数
			shipmentsRequestDTO.setToName("Victor Zhou");
			shipmentsRequestDTO.setToCity("Warszawa");
			shipmentsRequestDTO.setToState("Warsaw");
			shipmentsRequestDTO.setToAddress1("Emilii Plater Street 53");
			shipmentsRequestDTO.setToZip5("00-113");
			shipmentsRequestDTO.setToPhone("888000000");
		} else if("IT".equals(countryCode)) {
			// 意大利参数
			shipmentsRequestDTO.setToName("ANTONIO CONTE");
			shipmentsRequestDTO.setToCity("BERNATE TICINO");
			shipmentsRequestDTO.setToState("MILANO");
			shipmentsRequestDTO.setToAddress1("VIA MILANO N 1");
			shipmentsRequestDTO.setToZip5("20010");
			shipmentsRequestDTO.setToPhone("3343032930");
		} else if("BE".equals(countryCode)) {
			// 比利时参数
			shipmentsRequestDTO.setToName("Espace");
			shipmentsRequestDTO.setToCity("Brussels");
			shipmentsRequestDTO.setToState("");
			shipmentsRequestDTO.setToAddress1("5130 Halford Drive Building #C Unit 1");
			shipmentsRequestDTO.setToZip5("1000");
			shipmentsRequestDTO.setToPhone("1-519-737-9101");
		} else if("UK".equals(countryCode)) {
			// 英国参数
			shipmentsRequestDTO.setToName("Espace");
			shipmentsRequestDTO.setToCity("London");
			shipmentsRequestDTO.setToState("England");
			shipmentsRequestDTO.setToAddress1("Unit 2265,Unit 6,Charlton Gate Business Park,Anchor and Hope Lane,");
			shipmentsRequestDTO.setToZip5("SE7 7RU");
			shipmentsRequestDTO.setToPhone("+447897159358");
		} else if("BR".equals(countryCode)) {
			
			
			shipmentsRequestDTO.setFromCountryCode("BR");
			shipmentsRequestDTO.setFromName("KunKun");
			shipmentsRequestDTO.setFromAddress1("José Pedro Fernandes 329");
			shipmentsRequestDTO.setFromZip5("58400002");
			shipmentsRequestDTO.setFromCity("Campina Grande");
			shipmentsRequestDTO.setFromState("PB");
			shipmentsRequestDTO.setFromPhone("11970796992");
			
			
			// 巴西参数
			shipmentsRequestDTO.setToName("Espace");
			shipmentsRequestDTO.setToCity("Campina Grande");
			shipmentsRequestDTO.setToState("PB");
			shipmentsRequestDTO.setToAddress1("José Pedro Fernandes 328");
			shipmentsRequestDTO.setToZip5("58400002");
			shipmentsRequestDTO.setToPhone("83996112463");
		} else if("NL".equals(countryCode)) {
			shipmentsRequestDTO.setFromCountryCode("NL");
			shipmentsRequestDTO.setFromName("KunKun");
			shipmentsRequestDTO.setFromAddress1("Waldorpstraat");
			shipmentsRequestDTO.setFromZip5("2521CA");
			shipmentsRequestDTO.setFromCity("Den Haag");
			shipmentsRequestDTO.setFromState("-");
			shipmentsRequestDTO.setFromPhone("-");
			
			shipmentsRequestDTO.setToName("Espace");
			shipmentsRequestDTO.setToCity("Kerkrade");
			shipmentsRequestDTO.setToState("-");
			shipmentsRequestDTO.setToAddress1("Antwoordnummer");
			shipmentsRequestDTO.setToZip5("6460WB");
			shipmentsRequestDTO.setToPhone("-");
		}
		shipmentsRequestDTO.setToCountryCode(countryCode);
		
		shipmentsRequestDTO.setCustomerReferenceNo(UUID.fastUUID().toString(true));
		
//		shipmentsRequestDTO.setToFirm("FR-RTN-AE");
//		shipmentsRequestDTO.setWeight(new BigDecimal("10"));
//		shipmentsRequestDTO.setLength(new BigDecimal("20"));
//		shipmentsRequestDTO.setWidth(new BigDecimal("20"));
//		shipmentsRequestDTO.setHeight(new BigDecimal("30"));

		ApiRequestDTO<LabelApplyRequestDTO> request = getRequest(shipmentsRequestDTO);
		String jsonString = JSON.toJSONString(request);
		LOG.info("求入参，request=" + jsonString);
		String response = HttpUtil.post(labelUrl, jsonString);
		LOG.info("响应参数，response=" + response);
	}

	public static void doCancel() {
		LabelCancelRequestDTO cancelRequest = new LabelCancelRequestDTO();
		cancelRequest.setLabelNo("420191039205590289381600000292");
		ApiRequestDTO<LabelCancelRequestDTO> request = getRequest(cancelRequest);
		String jsonString = JSON.toJSONString(request);
		LOG.info("请求入参，request=" + jsonString);
		String response = HttpUtil.post(cancelUrl, jsonString);
		LOG.info("响应参数，response=" + response);
	}
	
	public static void doTrack() {
		LabelTrackRequestDTO trackRequest = new LabelTrackRequestDTO();
		List<String> labelNoList = new ArrayList<>();
		labelNoList.add("420191039205590289381600000292");
//		labelNoList.add("9202090256754700264916");
		trackRequest.setLabelNoList(labelNoList);
		ApiRequestDTO<LabelTrackRequestDTO> request = getRequest(trackRequest);
		String jsonString = JSON.toJSONString(request);
		LOG.info("请求入参，request=" + jsonString);
		String response = HttpUtil.post(trackUrl, jsonString);
		LOG.info("响应参数，response=" + response);
	}
	
	public static void doLabelResult() {
		LabelQryRequestDTO qryRequest = new LabelQryRequestDTO("5eab200c0cb74ae598e0cb8c288286fb");
		ApiRequestDTO<LabelQryRequestDTO> request = getRequest(qryRequest);
		String jsonString = JSON.toJSONString(request);
		LOG.info("请求入参，request=" + jsonString);
		String response = HttpUtil.post(queryUrl, jsonString);
		LOG.info("响应参数，response=" + response);
	}

	private static <T> ApiRequestDTO<T> getRequest(T parameter) {
		long time = System.currentTimeMillis();
		ApiRequestDTO<T> request = new ApiRequestDTO<>();
		request.setPartnerId(partnerId);
		try {
			String signature = DigestUtils
					.md5DigestAsHex((partnerKey + time + JSON.toJSONString(parameter)).getBytes("UTF-8"));
			request.setSignature(signature);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		request.setTime(time);
		request.setParameter(parameter);
		return request;
	}
}
